<?php
namespace Models;

use Utils\Database;

/**
 * 绿波路段模型类
 */
class GreenWaveSection {
    private $db;
    
    public function __construct() {
        $this->db = Database::getInstance();
    }
    
    /**
     * 获取所有绿波路段
     */
    public function getAllSections() {
        $sql = "SELECT * FROM green_wave_sections ORDER BY id ASC";
        return $this->db->query($sql)->fetchAll();
    }
    
    /**
     * 获取指定绿波路段信息
     */
    public function getSectionById($id) {
        $sql = "SELECT * FROM green_wave_sections WHERE id = ?";
        return $this->db->query($sql, [$id])->fetch();
    }
    
    /**
     * 添加新绿波路段
     */
    public function addSection($data) {
        $sql = "INSERT INTO green_wave_sections (
                    name, 
                    start_intersection_id, 
                    end_intersection_id,
                    direction,
                    design_speed,
                    status
                ) VALUES (?, ?, ?, ?, ?, ?)";
        return $this->db->query($sql, [
            $data['name'],
            $data['start_intersection_id'],
            $data['end_intersection_id'],
            $data['direction'],
            $data['design_speed'],
            $data['status'] ?? 1
        ]);
    }
    
    /**
     * 更新绿波路段信息
     */
    public function updateSection($id, $data) {
        $sql = "UPDATE green_wave_sections SET 
                name = ?, 
                start_intersection_id = ?, 
                end_intersection_id = ?,
                direction = ?,
                design_speed = ?,
                status = ?,
                updated_at = CURRENT_TIMESTAMP 
                WHERE id = ?";
        return $this->db->query($sql, [
            $data['name'],
            $data['start_intersection_id'],
            $data['end_intersection_id'],
            $data['direction'],
            $data['design_speed'],
            $data['status'],
            $id
        ]);
    }
    
    /**
     * 删除绿波路段
     */
    public function deleteSection($id) {
        $sql = "DELETE FROM green_wave_sections WHERE id = ?";
        return $this->db->query($sql, [$id]);
    }
    
    /**
     * 获取活跃绿波路段数量
     */
    public function getActiveSectionsCount() {
        $sql = "SELECT COUNT(*) as count FROM green_wave_sections WHERE status = 1";
        return $this->db->query($sql)->fetch()['count'];
    }
    
    /**
     * 获取路段的所有路口
     */
    public function getSectionIntersections($sectionId) {
        $sql = "SELECT i.* FROM intersections i 
                INNER JOIN section_intersections si ON i.id = si.intersection_id 
                WHERE si.section_id = ? 
                ORDER BY si.sequence ASC";
        return $this->db->query($sql, [$sectionId])->fetchAll();
    }
    
    /**
     * 添加路口到路段
     */
    public function addIntersectionToSection($sectionId, $intersectionId, $sequence) {
        $sql = "INSERT INTO section_intersections (section_id, intersection_id, sequence) 
                VALUES (?, ?, ?)";
        return $this->db->query($sql, [$sectionId, $intersectionId, $sequence]);
    }
    
    /**
     * 获取路段的当前优化效率
     */
    public function getSectionEfficiency($sectionId) {
        $sql = "SELECT efficiency FROM section_statistics 
                WHERE section_id = ? 
                ORDER BY created_at DESC LIMIT 1";
        $result = $this->db->query($sql, [$sectionId])->fetch();
        return $result ? $result['efficiency'] : 0;
    }
} 